<?php
include_once(dirname(__FILE__)."/../inc/common.php");

$accountClass = new tally_account();
$accountListTemp = $accountClass->findAccountList($userId, true);
$accountList = array();
foreach($accountListTemp as $account){
	$accountList[$account["id"]] = $account;
}

$tallyFlowTypeClass = new tally_flowType();
$flowTypeListTemp = $tallyFlowTypeClass->findAllFlowType($userId, true);
$flowTypeList = array();
foreach($flowTypeListTemp as $flowType){
	$flowTypeList[$flowType["id"]] = $flowType;
}

$tallyTagClass = new tally_tag();
$tagListTemp = $tallyTagClass->findTagByAccountId($userId, null, true, 0, -1);
$tagList = array();
foreach($tagListTemp as $tag){
	$tagList[$tag["id"]] = $tag;
}

$dateFrom = isset($_POST["dateFrom"])?$_POST["dateFrom"]:null;
$dateTo = isset($_POST["dateTo"])?$_POST["dateTo"]:null;
$orderField = isset($_POST["orderField"])?$_POST["orderField"]:"";
$inOrOut = isset($_POST["inOrOut"])?$_POST["inOrOut"]:"";
$tagIds = isset($_POST["tagId"])?$_POST["tagId"]:"";
$remark = isset($_POST["remark"])?$_POST["remark"]:"";
$categoryId = isset($_POST["categoryId"])?$_POST["categoryId"]:"";
$accountId = isset($_POST["subjectId"])?$_POST["subjectId"]:null;
$minAmount = isset($_POST["minAmount"])?$_POST["minAmount"]:null;
$maxAmount = isset($_POST["maxAmount"])?$_POST["maxAmount"]:null;
$curPage = isset($_GET["curPage"])?$_GET["curPage"]:1;
$pageSize = 15;

$flowClass = new tally_flow();
$flowList = $flowClass->findFlowByUserId($userId, $accountId, $categoryId, $dateFrom, $dateTo,
	$tagIds, $remark, $minAmount, $maxAmount, $orderField, ($curPage-1)*$pageSize, $pageSize, $inOrOut);
$total = $flowClass->countFlowByUserId($userId, $accountId, $categoryId, $dateFrom, $dateTo,
	$tagIds, $remark, $minAmount, $maxAmount, $inOrOut);
$nownum = $flowList?count($flowList):0;

$inCount = $flowClass->inCountByUserId($userId, $accountId, $categoryId, $dateFrom, $dateTo,
	$tagIds, $remark, $minAmount, $maxAmount, $orderField, $inOrOut);
$outCount = $flowClass->outCountByUserId($userId, $accountId, $categoryId, $dateFrom, $dateTo,
	$tagIds, $remark, $minAmount, $maxAmount, $orderField, $inOrOut);
$inOutDiff = $inCount-$outCount;
?>
	  <!--begin:数据列表-------------------------------------------------------------------------------------------------->
<table id="listtb" cellpadding="0" cellspacing="0" class="composite_table">
  <thead>
  <tr>
    <th width="12%">日期</th>
    <th width="52%">详情</th>
    <th width="12%">流入</th>
    <th width="12%">流出</th>
    <th width="12%">余额</th>
  </tr>
  </thead>
  <tbody>
  <?php
	foreach($flowList as $flow){
		$inout = "left";
		if($flow["moneyOut"]>0){
			$inout = "right";
		}

		$flowType = "";
		if(tally_flow::$INOUT_TRANSFER!=$flow["inOrOut"]){
			$flowType = isset($flowTypeList[$flow["flowType"]])?$flowTypeList[$flow["flowType"]]["name"]:'';
		}else{
			$flowType = $accountList[$flow["transferTargetAccountId"]]["name"];
		}

		$bizType = 0;
		switch($flow["inOrOut"]){
			case tally_flow::$INOUT_INCOMME:
				$bizType = 11;
				break;
			case tally_flow::$INOUT_EXPENSE:
				$bizType = 20;
				break;
			default:
				$bizType = 30;
				break;
		}

		$tradeTime = strtotime($flow["tradeTime"]);
		$mark = "";
		$tagIdList = $flowClass->findTagByFlow($userId, $flow["id"]);
		if($tagIdList){
			$tags = "";
			foreach($tagIdList as $tagMap){
				if($tags){
					$tags .= ",";
				}
				$tags .= $tagList[$tagMap["tagId"]]["name"];
			}
			if($tags) {
				$mark .= "[" . $tags . "]";
			}
		}
		if ($flow["desc"]){
			$mark .= $flow["desc"];
		}
	?>
	<tr>
		<td colspan="6"  class="td">
				<div class="list_line">
					<table class="listtable">
						<tr>
							<td rowspan="2" width="12%">
								<p title="<?php echo date("Y-m-d H:i", $tradeTime)?>">
									<?php echo date("Y-m-d", $tradeTime)?>
								</p>
							</td>
							<td width="52%" class="top dotted right_dotted">
							   	<span class="flow"><?php echo tally_flow::$INOUT_DESC[$flow["inOrOut"]]?>|<?php echo $accountList[$flow["accountId"]]["name"]?>
								   		<img src="../images/flow_<?php echo $inout?>.gif"
									align="absmiddle">
							   	<?php echo $flowType?>
								</span>
							</td>
							<td rowspan="2" class="number" width="12%">
								<?php echo $flow["moneyIn"]>0?$flow["moneyIn"]:''?>
							</td>
							<td rowspan="2" class="number" width="12%">
								<?php echo $flow["moneyOut"]>0?$flow["moneyOut"]:''?>
							</td>
							<td rowspan="2" class="number" width="12%">
								<?php echo $flow["remain"]?>
							</td>
					  	</tr>
					  	<tr>
					    	<td class="buttom">
					    		<?php echo $mark?>
							</td>
					  	</tr>


					</table>
					<div class="oper_bar">

							<span><a class="deleteBtn" title="删除" href="javascript:fGoto();" onclick="Tally.onDelete('<?php echo $flow["id"]?>');CM.afterSubmit=Cash.search;CM.afterSubmit0=Tally.loadBoard;">删除</a></span>
							<span><a title="修改" href="javascript:fGoto();" onclick="Tally.goEdit('<?php echo $flow["id"]?>','<?php echo $flow["inOrOut"]?>','<?php echo $bizType?>','0');CM.afterSubmit=Cash.search;CM.afterSubmit0=Tally.loadBoard;">修改</a></span>

                              <span><a title="复制" href="javascript:fGoto();" onclick="Tally.goEdit('<?php echo $flow["id"]?>','<?php echo $flow["inOrOut"]?>','<?php echo $bizType?>','1');CM.afterSubmit=Cash.search;CM.afterSubmit0=Tally.loadBoard;">复制</a></span>
                    </div>
				</div>

		</td>
		</tr>
	<?php } ?>


  </tbody>

   <tfoot>
   <tr>
   	<td colspan="10"><span class=pagebar><?php echo isspage($total,$nownum,$pageSize,$curPage); ?></span></td>
   </tr>
   </tfoot>
</table>

<div id="account_static">
	<div class="total">
		<span>现金流入:<font class="num"><?php echo $inCount?></font></span>
		<span>现金流出:<font class="num"><?php echo $outCount?></font></span>
		<span>差额:<font class="num"><?php echo $inOutDiff?></font></span>
	</div>
</div>
